//
//  BIDReplyListViewController.m
//  cxgov
//
//  Created by mal on 16/3/7.
//  Copyright © 2016年 mal. All rights reserved.
//

#import "BIDReplyListViewController.h"
#import "BIDNoticeReplyCell.h"

//回复列表
static NSString *replyListURL = @"noticeAndroid!listReplied.action";

@interface BIDReplyListViewController ()<BIDBaseListViewDelegate>
{
    BIDNoticeReplyCell *_temporaryCell;
    BOOL _bRegister;
}
@end

@implementation BIDReplyListViewController
@synthesize noticeId;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"回复列表";
    // Do any additional setup after loading the view.
    _temporaryCell = [[[NSBundle mainBundle] loadNibNamed:@"BIDNoticeReplyCell" owner:self options:nil] lastObject];
    //
    self.myTableView.allowsSelection = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    self.curPage = 0;
    [self.dataSourceArr removeAllObjects];
    [self loadData];
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], replyListURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"noticeReply.noticeId=%@&page=%ld&rows=%ld", noticeId, self.curPage, self.countPerPage];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(responseDic)
        {
            [self.dataSourceArr addObjectsFromArray:responseDic[@"rows"]];
            if(self.dataSourceArr.count<self.curPage*self.countPerPage)
            {
                [self.myTableView hasNoMoreData];
            }
            [self.myTableView reloadData];
        }
    }];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    NSUInteger row = indexPath.row;
    NSUInteger section = indexPath.section;
    static NSString *identifier = @"identifier";
    NSDictionary *dic = self.dataSourceArr[row];
    if(!_bRegister)
    {
        _bRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDNoticeReplyCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDNoticeReplyCell *noticeReplyCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    noticeReplyCell.nameLabel.text = dic[@"receiveUserName"];
    noticeReplyCell.dateLabel.text = [[NSString alloc] initWithFormat:@"时间:%@", dic[@"replyTime"]];
    noticeReplyCell.replyContentLabel.text = [[NSString alloc] initWithFormat:@"回复:%@", dic[@"content"]];
    cell = noticeReplyCell;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.dataSourceArr[indexPath.row];
    _temporaryCell.replyContentLabel.preferredMaxLayoutWidth = CGRectGetWidth(tableView.frame)-47-8;
    _temporaryCell.replyContentLabel.text = [[NSString alloc] initWithFormat:@"回复:%@", dic[@"content"]];
    CGSize size = [_temporaryCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    return size.height+1;
}

@end
